#!/bin/bash
script_path=$(realpath $(dirname ${BASH_SOURCE[0]}))
bash_dir=$(dirname "$script_path")

host=`uname -a`
echo $host

llvm_version="LLVM-20.1.1-Linux-ARM64"

check_and_download_llvm() {
# 检查 CLANG_BASE_PATH 是否已设置
  if [ -z "$CLANG_BASE_PATH" ]; then
    echo "Error: CLANG_BASE_PATH is not set!"
    echo "You need to export it, and it should be an absolute path, like: /home/huanghan/LLVM-20.1.1-Linux-ARM64/"
    exit -1  # 返回错误代码 1，表示失败
  else
    echo "CLANG_BASE_PATH is set to: $CLANG_BASE_PATH"
  fi
}


if [[ ${host} == *"aarch64"* ]]; then
    arch=arm64
    check_and_download_llvm
    mkdir -p ${bash_dir}/out/arm64.release-check
    gn gen ${bash_dir}/out/arm64.release-check --args="
        is_component_build = false
        is_debug = false
        target_cpu = \"arm64\"
        v8_target_cpu = \"arm64\"
        clang_base_path = \"${CLANG_BASE_PATH}\"
        clang_use_chrome_plugins = false
        v8_enable_sandbox = true
        v8_enable_sandbox = false
        v8_enable_backtrace = true
        v8_enable_disassembler = true
        v8_enable_object_print = true
        v8_enable_verify_heap = true
        dcheck_always_on = true
        treat_warnings_as_errors = false"

else
    arch=x64
    mkdir -p ${bash_dir}/out/x64.release-check
    gn gen ${bash_dir}/out/x64.release-check --args="
        is_component_build = false
        is_debug = false
        target_cpu = \"x64\"
        v8_enable_sandbox = true
        v8_enable_sandbox = false
        v8_enable_backtrace = true
        v8_enable_disassembler = true
        v8_enable_object_print = true
        v8_enable_verify_heap = true
        dcheck_always_on = true"
fi

ninja -C ${bash_dir}/out/${arch}.release-check d8